package com.xxx.mch.controller;

import com.xxx.mch.domain.dto.DepartmentScoreStatisticsDto;
import com.xxx.mch.domain.dto.PersonScoreDto;
import com.xxx.mch.domain.po.TotalScore;
import com.xxx.mch.domain.request.QueryScoreRequest;
import com.xxx.mch.domain.request.UpdateTotalScoreRequest;
import com.xxx.mch.service.TotalScoreService;
import com.xxx.mch.utils.WrapMapper;
import com.xxx.mch.utils.Wrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author FengHaixin
 * @description 一句话描述该文件的用途
 * @date 2020/3/4
 */
@RestController
@RequestMapping ("totalScore")
public class TotalScoreController
{

    @Autowired
    private TotalScoreService totalScoreService;


    /**
     * @param queryScoreRequest String：yyyy-MM格式
     */
    @GetMapping (produces = MediaType.APPLICATION_JSON_VALUE)
    public Wrapper<PersonScoreDto> getList(@ModelAttribute @Validated QueryScoreRequest queryScoreRequest)
    {
        //FIXME uid从登陆信息中获取
        return WrapMapper.ok(totalScoreService.queryTotalScore(queryScoreRequest));
    }


    @GetMapping (value = "deptAvg", produces = MediaType.APPLICATION_JSON_VALUE)
    public Wrapper<List<DepartmentScoreStatisticsDto>> deptAvg(@RequestParam ("month") String month)
    {
        return WrapMapper.ok(totalScoreService.departmentScoreStatistics(month));
    }


    @PutMapping (produces = MediaType.APPLICATION_JSON_VALUE)
    public Wrapper updateTotalScore(@RequestBody @Validated UpdateTotalScoreRequest request)
    {
        TotalScore totalScore = new TotalScore(true);
        BeanUtils.copyProperties(request, totalScore);
        totalScoreService.updateTotalScore(totalScore);
        return WrapMapper.ok();
    }
}
